package com.c.ctgproject.adapter.service

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.R
import com.c.ctgproject.entity.service.ServiceEditCheckBean

class ServiceEditCheckAdapter(private val context: Context, private val mList: List<ServiceEditCheckBean>, private val listener: ServiceEditCallback) : RecyclerView.Adapter<ServiceEditCheckAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_service_check_list, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data = mList[position]
        holder.mTextView.text = data.name
        holder.mCheckBox.isChecked = data.select
        holder.mParentLL.setOnClickListener {
            holder.mCheckBox.performClick()
        }
        holder.mCheckBox.setOnClickListener {
            listener.onItemSelect(position, holder.mCheckBox.isChecked)
        }
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val mCheckBox: CheckBox = itemView.findViewById(R.id.mCheckBox)
        val mTextView: TextView = itemView.findViewById(R.id.mTextView)
        val mParentLL: LinearLayoutCompat = itemView.findViewById(R.id.mParentLL)
    }

    interface ServiceEditCallback {
        fun onItemSelect(position: Int, select: Boolean)
    }
}